package com.zjsos.governmentaffairs.sokettestapplication.webSocket;


import com.zjsos.governmentaffairs.sokettestapplication.MyApplication;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Response;
import okio.ByteString;

public class WebServiceClient {


//    private static String address = "ws://47.99.36.61:8080/";
    private static String address = "ws://47.99.36.61:8181/";
//    private static String address = "ws://192.168.0.53:8080/";
    private static final String TAG = "JavaWebSocket";
    private static WebServiceClient client;
    private WsManager wsManager;

    public static WebServiceClient getInstance(){
        if (client==null){
            client=new WebServiceClient();
        }
        return client;
    }


    public void initSocket(){

        if (wsManager != null) {
            wsManager.stopConnect();
            wsManager = null;
        }
        wsManager = new WsManager.Builder(MyApplication.getAppContext())
                .client(new OkHttpClient().newBuilder()
                                .pingInterval(15, TimeUnit.SECONDS)
                                .retryOnConnectionFailure(true)
                                .build())
                .needReconnect(true)
                .wsUrl(address)
                .build();
        wsManager.setWsStatusListener(new WsStatusListener() {
            @Override
            public void onOpen(Response response) {
                super.onOpen(response);
            }

            @Override
            public void onMessage(String text) {
                super.onMessage(text);
                EventBus.getDefault().post(text);
            }

            @Override
            public void onMessage(ByteString bytes) {
                super.onMessage(bytes);
            }

            @Override
            public void onReconnect() {
                super.onReconnect();
            }

            @Override
            public void onClosing(int code, String reason) {
                super.onClosing(code, reason);
            }

            @Override
            public void onClosed(int code, String reason) {
                super.onClosed(code, reason);
            }

            @Override
            public void onFailure(Throwable t, Response response) {
                super.onFailure(t, response);
            }
        });
        wsManager.startConnect();
    }


    public void postMessage(JSONArray array){
        JSONObject object = new JSONObject();
        try {
            object.put("cmd", "tick@subscribe");
            object.put("instrumentIds",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (wsManager==null){
            initSocket();
        }
        if (!wsManager.isWsConnected()){
            wsManager.startConnect();
        }
        wsManager.sendMessage(object.toString());
    }


//    public void sendMessage(byte[] msg){
//        if (wsManager==null){
//            initSocket();
//        }
//        if (!wsManager.isWsConnected()){
//            wsManager.startConnect();
//        }
//        wsManager.sendMessage(msg);
//    }


    public void disConnect(){
        if (wsManager!=null){
            wsManager.stopConnect();
            wsManager=null;
        }
    }


}
